package com.daiji.dao.impl;

import com.daiji.dao.UserDao;
import com.daiji.entity.UserEntity;
import com.daiji.util.JDBCUtil;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {

    @Override
    public UserEntity select(String username, String password) {
        String sql = "select * from user where username=? and password=?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, username, password);

        if (maps.size()>0){
            Map<String, Object> map = maps.get(0);

            UserEntity user = new UserEntity();
            try {
                BeanUtils.populate(user,map);
                return user;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
